﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Utils
{
    public class Speech
    {
        public static string ReadActiveDocument(bool async = true, string lang = "zh-cn")
        {
            try
            {
                var ae = Globals.MainWindow.ActiveTextEditor;
                if (ae == null) return "没有活动文档。";

                var text = ae.Text;

                return ReadText(text, async, lang);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static string ReadSelection(bool async = true, string lang = "zh-cn")
        {
            try
            {
                var ae = Globals.MainWindow.ActiveTextEditor;
                if (ae == null) return "没有活动文档。";

                return ReadText(ae.SelectedText, async, lang);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static string ReadActiveLineByRegex(string regex, bool async = true, string lang = "zh-cn")
        {
            try
            {
                var ae = Globals.MainWindow.ActiveTextEditor;
                if (ae == null) return "没有活动文档。";

                var line = ae.Document.GetLineByOffset(ae.SelectionStart);
                var text = ae.Document.GetText(line.Offset, line.Length);

                var reg = new Regex(regex);
                var match = reg.Match(text);
                if (match.Success == false) return "当前行不能匹配指定规则。";

                return ReadText(match.Value, async, lang);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static string ReadActiveLine(bool async = true, string lang = "zh-cn")
        {
            try
            {
                var ae = Globals.MainWindow.ActiveTextEditor;
                if (ae == null) return "没有活动文档。";

                var line = ae.Document.GetLineByOffset(ae.SelectionStart);
                var text = ae.Document.GetText(line.Offset, line.Length);

                return ReadText(text, async, lang);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static string[] GetTextArrayByRegexInActiveDocument(string regex)
        {
            try
            {
                var ae = Globals.MainWindow.ActiveTextEditor;
                if (ae == null) return null;
                if (string.IsNullOrWhiteSpace(ae.Text)) return null;

                Regex reg = new Regex(regex);
                var matches = reg.Matches(ae.Text);
                if (matches.Count <= 0) return null;

                var array = new string[matches.Count];
                for (int i = 0; i < matches.Count; i++)
                {
                    var match = matches[i] as Match;
                    if (match == null) continue;

                    array[i] = match.Value;
                }
                return array;
            }
            catch
            {
                return null;
            }
        }

        public static string ReadByRegexInActiveDocument(string regex, bool async = true, string lang = "zh-cn")
        {
            try
            {
                var ae = Globals.MainWindow.ActiveTextEditor;
                if (ae == null) return "没有活动文档。";
                if (string.IsNullOrWhiteSpace(ae.Text)) return "活动文档中未找到可供朗读的文本。";

                Regex reg = new Regex(regex);
                var matches = reg.Matches(ae.Text);
                if (matches.Count <= 0) return "活动文档中未找到符合规则的、可供朗读的文本。";

                StringBuilder sb = new StringBuilder();
                foreach (var m in matches)
                {
                    var match = m as Match;
                    sb.Append(match.Value);
                    sb.Append("\r\n");
                }

                return ReadText(sb.ToString(), async, lang);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static string ReadText(string text, bool async = true, string lang = "zh-cn")
        {
            try
            {
                if (string.IsNullOrWhiteSpace(text)) return "未找到要朗读的文本。";

                string textToSpeak = text;
                SpeechSynthesizer synthes = new SpeechSynthesizer();
                var voices = synthes.GetInstalledVoices(new System.Globalization.CultureInfo(lang));
                if (voices.Count > 0)
                {
                    var voice = voices[0];
                    synthes.SelectVoice(voice.VoiceInfo.Name);
                }

                if (async)
                {
                    synthes.SpeakAsync(textToSpeak);    //异步
                }
                else
                {
                    synthes.Speak(textToSpeak);         //同步
                }

                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
